package com.yinyong;

/**
 * Time    : 2023/3/18 22:31
 * Author  : 王摇摆
 * FileName: TestHouse1.java
 * Software: IntelliJ IDEA 2020.2.2
 * Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
 */

import javax.sound.midi.Soundbank;
import java.sql.SQLOutput;

/**
 * Java中的经典对象引用案例
 * 人有一间房
 * 房属于一个人
 */
public class TestHouse1 {
    public static void main(String[] args) {
        Person1 person = new Person1("202207034", "wgw");
        House1 house = new House1("18", "wenying");

        person.setHouse(house);
        house.setPerson(person);

        System.out.println("程序初始化完毕！结果如下：");
//        person.getPersonInfo();//get了有啥用，返回的是一个string类型的数据，这里的执行结果其实是一个字符串，但是有啥用啊
//        house.getHouseInfo();
        System.out.println(person.toString());
        System.out.println(house.toString());

        System.out.println("经典案例的实现:");
        System.out.println("人有一间房：" + person.getHouse().getHouseInfo());
        System.out.println("房属于一个人：" + house.getPerson().getPersonInfo());


    }
}

class Person1 {
    private String no;
    private String name;
    private House1 house;

    public void setHouse(House1 house) {
        this.house = house;
    }

    public Person1(String no, String name) {
        this.no = no;
        this.name = name;
    }

    public Person1() {
        System.out.println("person1的无参构造器执行了");
    }

    public String getPersonInfo() {
//        System.out.println(this.toString());
        return "ID:" + this.no + "  name:" + this.name;
    }

    @Override
    public String toString() {
        return "Person1{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public House1 getHouse() {
        return this.house;
    }
}


class House1 {
    private String no;
    private String name;
    private Person1 person;

    public void setPerson(Person1 person) {
        this.person = person;
    }

    public House1(String no, String name) {
        this.no = no;
        this.name = name;
    }

    public String getHouseInfo() {
//        System.out.println(toString());//隐式调用当前类对象的tostring方法
        return "房号：" + this.no + "  房名： " + this.name;
    }

    @Override
    public String toString() {
        return "House1{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public Person1 getPerson() {
        return person;
    }
}

